/** \file gr_map_assign.h */
#pragma once


#include <map>


namespace gr{

template<class K, class V>
class MapAssignHelper
{
public:
    std::map<K,V>& map_;
    MapAssignHelper(std::map<K,V>& map):map_(map){}
    MapAssignHelper& operator()(const K& key, const V& value)
    {
        map_[key] = value;
        return *this;
    }
    std::map<K,V>& operator()(){return map_;}
};

template<class K, class V>
class MapAssign
{
public:

    std::map<K,V> accum;
    MapAssignHelper<K,V> helper;

    MapAssign(std::map<K,V>& map):helper(map){}
    MapAssign(const K& key, const V& value):helper(accum){accum[key] = value;}
    MapAssignHelper<K,V>& operator()(const K& key, const V& value){ return helper(key, value);}
    std::map<K,V>& operator()(){return helper.map_;}
};

/** Usage:
    std::map m;
    mapAssign(m)(key1,value1)(key2, value2)...;
*/
template<class K, class V>
MapAssign<K,V> mapAssign(std::map<K,V>& map){return MapAssign<K,V>(map);}


/** Usage:
    std::map m = mapAssign(key1,value1)(key2, value2)...;
*/
template<class K>
MapAssign<K,const char* > mapAssign(const K& key, const char*value)
{
    return MapAssign<K,const char*>(key, value);
}

template<class K, class V>
MapAssign<K,V> mapAssign(const K& key, const V& value)
{
    return MapAssign<K,V>(key, value);
}

}
